package com.jackeysun.widget

import android.graphics.*
import android.graphics.drawable.Drawable


class CircleDrawable(
    /*var progressColor: Int = Color.RED,
    var progressPaintWidth: Float = 10f,
    var startAngle: Int = 0,*/
    var backgroundPaintWidth: Float = 10f,
    var backgroundColor: Int = Color.GRAY,
    var solidLength: Int = 4,
    var dottedLength: Int = 4,
    var intervalLength: Int = 0,
    var isSolid: Boolean = true

    ) : Drawable() {
//    private var progressPaint = Paint()
    private var backgroundPaint = Paint()
    private var backgroundRectF = RectF()
    private var mRadius: Float = 10f

    init {
//        progressPaint.style = Paint.Style.STROKE
//        backgroundPaint.isAntiAlias = true//抗锯齿
//        progressPaint.isDither = true//防抖动
////        progressPaint.strokeCap = Paint.Cap.ROUND
//        progressPaint.color = progressColor
//        progressPaint.strokeWidth = progressPaintWidth
//        progressPaint.pathEffect = DashPathEffect(floatArrayOf(4f, 4f), 0f)

        backgroundPaint.style = Paint.Style.STROKE
        backgroundPaint.isAntiAlias = true//抗锯齿
        backgroundPaint.isDither = true//防抖动
        backgroundPaint.color = backgroundColor
        backgroundPaint.strokeWidth = backgroundPaintWidth
        if (isSolid) {//实线
            backgroundPaint.strokeCap = Paint.Cap.ROUND
        } else {//虚线
            //绘制长度为4的实线后再绘制长度为4的空白区域，0位间隔
            backgroundPaint.pathEffect = DashPathEffect(
                floatArrayOf(solidLength.toFloat(), dottedLength.toFloat()),
                intervalLength.toFloat()
            )
        }
    }

    override fun onBoundsChange(bounds: Rect) {
        super.onBoundsChange(bounds)
        backgroundRectF = RectF(bounds)
        mRadius = backgroundRectF.height().coerceAtMost(backgroundRectF.width()).div(2)
    }

    override fun draw(canvas: Canvas) {

//        canvas.save()//保存当前状态
        canvas.translate(backgroundPaintWidth.div(2), backgroundPaintWidth.div(2))//平移
        canvas.drawCircle(
            mRadius.minus(backgroundPaintWidth.div(2)),
            mRadius.minus(backgroundPaintWidth.div(2)),
            mRadius.minus(backgroundPaintWidth.div(2)),
            backgroundPaint
        )
//
//        canvas.restore()//恢复上一次状态
//        canvas.translate(progressPaintWidth.div(2), progressPaintWidth.div(2))
//        val sweepAngle: Float = progress.toFloat().div(max).times(360)
//        canvas.drawArc(
//            RectF(
//                0f,
//                0f,
//                mRadius.times(2).minus(progressPaintWidth),
//                mRadius.times(2).minus(progressPaintWidth)
//            ),
//            startAngle.toFloat(),
//            sweepAngle,
//            false,
//            progressPaint
//        )
    }

    override fun setAlpha(alpha: Int) {
        backgroundPaint.alpha = alpha
    }

    override fun getOpacity(): Int {
        return PixelFormat.TRANSLUCENT
    }

    override fun setColorFilter(colorFilter: ColorFilter?) {
        backgroundPaint.colorFilter = colorFilter
    }
}